<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>器械采购</title>
<link rel="stylesheet" type="text/css"
	href="<c:url value="/css/themes/default/easyui.css"/>">
<link rel="stylesheet" type="text/css"
	href="<c:url value="/css/themes/icon.css"/>">
<script type="text/javascript" src="<c:url value="/js/jquery.min.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/js/jquery.easyui.min.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/js/easyui-lang-zh_CN.js"/>"></script>
</head>

<body>
<script type="text/javascript">

	var editIndex = undefined;
	//结束正在编辑的行
	function endEditing() {
		if (editIndex == undefined) {
			return true
		}
		if ($('#thpqxcg').datagrid('validateRow', editIndex)) {
			$('#thpqxcg').datagrid('endEdit', editIndex);
			editIndex = undefined;
			return true;
		} else {
			return false;
		}
	}
	//事件：单击单元格的事件
	function onClickCell(index, field) {
		if (editIndex != index) {
			if (endEditing()) {
				$('#thpqxcg').datagrid('selectRow', index).datagrid('beginEdit',
						index);
				var ed = $('#thpqxcg').datagrid('getEditor', {
					index : index,
					field : field
				});
				if (ed) {
					($(ed.target).data('textbox') ? $(ed.target).textbox(
							'textbox') : $(ed.target)).focus();
				}
				editIndex = index;
			} else {
				setTimeout(function() {
					$('#thpqxcg').datagrid('selectRow', editIndex);
				}, 0);
			}
		}
	}

	//结束编辑，同步数据
	function onEndEdit(index, row) {
		var ed = $(this).datagrid('getEditor', {
			index : index,
			field : 'cgUnit'
		});
		if(ed){
			row.cgUnit = $(ed.target).combobox('getText');
		}else{
			row.cgUnit = null;
		}
	}

	//添加行
	function append() {
		if (endEditing()) {
			var today='${PurchaseId }'*1;
			$('#thpqxcg').datagrid('appendRow', {
				'tHisPurchases.cgNo' : '',
				'tHisPurchases.cpName' : '',
				'tHisPurchases.cgNumber' : '',
				'tHisPurchases.cgUnit' : '',
				'tHisPurchases.cgPrice' : '',
				'tHisPurchases.cgTotalprice' : '',
				'tHisPurchases.cgBatchnumber' : '',
				'tHisPurchases.cgRemarks' : ''
			});
			editIndex = $('#thpqxcg').datagrid('getRows').length - 1;
			$('#thpqxcg').datagrid('selectRow', editIndex);
			$('#thpqxcg').datagrid('beginEdit',editIndex);
		}
	}

	//删除行
	function removeit() {
		if (editIndex == undefined) {
			return
		}
		$('#thpqxcg').datagrid('cancelEdit', editIndex).datagrid('deleteRow',
				editIndex);
		editIndex = undefined;
	}

	//应用
	function accept() {
		if (endEditing()) {
			//$('#thpqxcg').datagrid('acceptChanges');
			endEditing();
		}
	}

	//撤消
	function reject() {
		$('#thpqxcg').datagrid('rejectChanges');
		editIndex = undefined;
	}

	//获取数据
	function getChanges() {
		var rows = $('#thpqxcg').datagrid('getChanges');
		alert(rows.length + ' rows are changed!');
	}
	
$(function(){
	//器械自动赋值
	 $('#cgqx').combogrid({ 
         onChange: function (newValue, oldValue) {  
            $.getJSON("findHisApparatusByqxId", {"qxNo": newValue}, function(data){
            	//批号赋值
				$('#qxPh').textbox('setValue',data.qxPh);
				$('#qxPrice').numberbox('setValue',data.qxPrice);
				
				$('#qxPrice').numberbox({
					onChange: function (newValue, oldValue) {  
						var n=$('#cgNumber').numberbox('getValue');
						$('#cgTotalprice').numberbox('setValue',newValue*n);
					}
				});
				
				$('#cgNumber').numberbox({
					onChange: function (newValue, oldValue) {  
						$('#cgTotalprice').numberbox('setValue',data.qxPrice*newValue);
					}
				});
			});
         }  
     });
	
	//采购器械加入采购表
	$('#add-cgxq').linkbutton({
		onClick:function(){
			var cgqx=$('#cgqx').combogrid('getText');
			var qxUnit=$('#qxUnit').textbox('getValue');
			var qxPh=$('#qxPh').textbox('getValue');
			var qxPrice=$('#qxPrice').numberbox('getValue');
			var cgNumber=$('#cgNumber').numberbox('getValue');
			var cgTotalprice=$('#cgTotalprice').numberbox('getValue');
			var cgRemarks=$('#cgRemarks').textbox('getValue');
			var rows=$('#thpqxcg').datagrid('getRows');
			var today='${PurchaseId }'*1;
			var ii=0;
			for(var i=0;i<rows.length;i++){
				//alert(cgqx);
				//var name=rows[i].tHisPurchases.cpName;
				//alert(name);
				//if(name==cgqx){
					//ii++;
				//}
			}
			if(ii>0){
				$.messager.alert("提示", "产品已存在采购表中，请勿重复添加!");
			}else{
				$('#thpqxcg').datagrid('appendRow', {
					'tHisPurchases.cgNo' : today+rows.length,
					'tHisPurchases.cpName' : cgqx,
					'tHisPurchases.cgNumber' : cgNumber,
					'tHisPurchases.cgUnit' : qxUnit,
					'tHisPurchases.cgPrice' : qxPrice,
					'tHisPurchases.cgTotalprice' : cgNumber,
					'tHisPurchases.cgBatchnumber' : qxPh,
					'tHisPurchases.cgRemarks' : cgRemarks
				});	
				$('#thpqxxq').form("clear");
			}
		}
	});
	
	//提交采购计划
	$('#submit-QiXiePurchase').linkbutton({
			onClick : function(){
			//将datagrid中的数据行保存到隐式表单域中
			//1、获取新增的数据行
			var rows = $('#thpqxcg').datagrid('getChanges');
			//为每一行生成对应的隐藏表单域
			var ddate=$('#ddate').datebox('getText');
			var SupplierNo=$('#SupplierNo').combogrid('getValue');
			var EmployeeNo=$('#EmployeeNo').combogrid('getValue');
			//alert(ddate);
			var ff_plan2=$("#ff_plan");
			for(var i = 0; i < rows.length; i ++){
				$("<input type='hidden' x='y'>").attr("name",
						"tHisPurchases[" + i + '].cgOrderdate').val(
						ddate).appendTo(ff_plan2);
				$("<input type='hidden' x='y'>").attr("name",
						"tHisPurchases[" + i + '].gysNo').val(
								SupplierNo*1).appendTo(ff_plan2);
				$("<input type='hidden' x='y'>").attr("name",
						"tHisPurchases[" + i + '].ygNo').val(
								EmployeeNo*1).appendTo(ff_plan2);
				generateHidden(rows[i], "cgNo", i);
				generateHidden(rows[i], "cpName", i);
				generateHidden(rows[i], "cgNumber", i);
				generateHidden(rows[i], "cgUnit", i);
				generateHidden(rows[i], "cgPrice", i);
				generateHidden(rows[i], "cgTotalprice", i);
				generateHidden(rows[i], "cgBatchnumber", i);
				generateHidden(rows[i], "cgRemarks", i);
			} 
			//提交表单
			if(rows.length != 0){
				$('#ff_plan').form('submit', {
					success: function(v){
						$(":hidden[x='y']").remove();
						$('#thpqxcg').datagrid('loadData', { total: 0, rows: [] });
						$.messager.alert('提示', '采购成功！');
						$('#ff_plan').form("clear");
						$("#win-purchase").window("close");
					}
				});
			}
		}
	});
	 
	 function generateHidden(row, name, i) {
		var ff_plan = $("#ff_plan");
		$("<input type='hidden' x='y'>").attr("name",
				"tHisPurchases[" + i + '].'  + name).val(
				row['tHisPurchases.'+name]).appendTo(ff_plan);
	}
})
</script>
<style>
	*{
		font-size:12px;
	}
	#purchaseContent{
		padding-bottom: 10px;
	}
	#purchasePlan{
		width:100%;
		overflow: hidden;
	}
	 #thpqxcgLI{
		float: left;
		list-style: none;
	}
	#thpqxxq{
		padding-left:20px;
	}
</style>
	<!-- 器械采购 -->
	<!-- <div id="win-purchase" class="easyui-window" title="器械采购" data-options="collapsible:false,
		minimizable:false,
		maximizable:false,
		modal:true,
		iconCls:'icon-purchase'"
		style="width: 806px; height: auto;"> -->
		<!-- <div id="titleBar">
			<a class="easyui-linkbutton" style="width:80px;">新建</a>
			<a class="easyui-linkbutton" style="width:80px;">打开</a>
			<a class="easyui-linkbutton" style="width:80px;">挂单</a>
			<a class="easyui-linkbutton" style="width:80px;margin-left:7%;">采购处理</a>
			<a class="easyui-linkbutton" style="width:80px;margin-left:14%;">报表</a>
			<a class="easyui-linkbutton" style="width:80px;">打印</a>
			<a class="easyui-linkbutton" style="width:80px;margin-left:4%;">关闭</a>
		</div> -->
		<form id="ff_plan" action="QiXiePurchase" method="post">
			<div id="purchaseContent">
				<div id="purchasePlan" style="background-color: rgb(236,243,255)">
					<ul>
						<li id="thpqxcgLI" style="margin:0px 60px 0px 0px;">采购单号&nbsp;<input name="tHisPurchaseplan.cgjhNo" class="easyui-textbox" data-options="width: 150" readonly="readonly"
							value="${PurchaseId }"></li>
						<li id="thpqxcgLI" style="margin:0px 0px 0px 60px;">状态&nbsp;<select name="tHisPurchases.cgState" class="easyui-combobox" data-options="width: 80" readonly="readonly">
							<option value="未采购" selected="selected">未采购</option><option value="已采购">已采购</option></select></li>
						<li id="thpqxcgLI" style="margin:0px 0px 0px 125px;">订货日期&nbsp;<input id="ddate" name="tHisPurchases.cgOrderdate" class="easyui-datebox" data-options="editable:false,width: 100,required: true"></li>
						<li id="thpqxcgLI" style="margin:5px 290px 10px 12px;">供应商&nbsp;<input id="SupplierNo" name="tHisPurchases.gysNo" class="easyui-combogrid" data-options="width: 150,
							url:'findHisSupplier',
							required: true,
							idField:'gysNo',
							panelHeight:200,
							textField:'gysName',
							columns:[[
					            {field:'gysName',title:'供应商',width:148}
							]]"></li>
						<li id="thpqxcgLI" style="margin:5px 0px 10px 74px;">采购员&nbsp;<select id="EmployeeNo" name="tHisPurchases.ygNo" class="easyui-combogrid" data-options="width: 100,
							url:'findAllEmployee',
							idField:'ygNo',
							panelHeight:200,
							panelWidth:225,
							required: true,
							textField:'ygName',
							columns:[[
					            {field:'ygName',title:'姓名',width:50},
					            {field:'bmName',title:'部门',width:75},
					            {field:'ygPost',title:'职务',width:92}
							]]"></select></li>
					</ul>
				</div>
				<div class="easyui-tabs"  data-options="border:false" style="width:100%;height:285px;overflow: hidden;">
				    <div title="采购订单" style="display:none;overflow: hidden;">
				        <div class="easyui-layout"  data-options="fit:true,border:false">
				        	<div id="thpqxxq" data-options="region:'north',border:false">
				        		<ul>
									<li id="thpqxcgLI" style="margin:0px 20px 10px 0px;">器械&nbsp;<select id="cgqx" class="easyui-combogrid" data-options="width: 150,
										url:'findApparatus',
										idField:'qxNo',
										panelHeight:200,
										textField:'cpName',
										columns:[[
								            {field:'cpName',title:'器械名称',width:'146px'}
										]]
										"></select></li>
									<li id="thpqxcgLI" style="margin:0px 20px 10px 0px;">单位&nbsp;<input id="qxUnit" class="easyui-textbox" data-options="width: 80"></li>
									<li id="thpqxcgLI" style="margin:0px 20px 10px 0px;">数量&nbsp;<input id="cgNumber" class="easyui-textbox" data-options="width: 80"></li>
									<li id="thpqxcgLI" style="margin:0px 20px 10px 0px;">单价&nbsp;<input id="qxPrice" class="easyui-numberbox" data-options="width: 80"></li>
									<li id="thpqxcgLI" style="margin:0px 0px 10px 0px;">总金额&nbsp;<input id="cgTotalprice" class="easyui-numberbox" readonly="readonly" data-options="width: 80,precision:2"></li>
									<li id="thpqxcgLI" style="margin:0px 20px 10px 0px;">批号&nbsp;<input id="qxPh" class="easyui-numberbox" data-options="width: 150"></li>
									<li id="thpqxcgLI" style="margin:0px 0px 10px 0px;">备注&nbsp;<input id="cgRemarks" class="easyui-textbox" data-options="width: 365"></li>
									<li id="thpqxcgLI" style="margin:0px 0px 10px 30px;"><a id="add-cgxq" class="easyui-linkbutton" style="width:80px;">加入</a></li>
								</ul>
				        	</div>
				        	<div data-options="region:'center',border:false">
				        		<table id="thpqxcg" class="easyui-datagrid"
									data-options="
										fitColumns:true,
										fit:true,
										singleSelect: true,
										rownumbers: true,
										toolbar: '#qxPurchaseTB',
										onClickCell: onClickCell,
										onEndEdit: onEndEdit,
										border:false
										">
									<thead>
										<tr>
											<th data-options="field:'tHisPurchases.cgNo',width:'13%',editor:'textbox'">id</th>
											<th data-options="field:'tHisPurchases.cpName',width:'20%',editor:'textbox'">器械名称</th>
											<th data-options="field:'tHisPurchases.cgNumber',width:'8%',editor:'textbox'">数量</th>
											<th data-options="field:'tHisPurchases.cgUnit',width:'10%',editor:{
												type:'combobox',
												options:{
													valueField:'label',
													textField:'value',
													panelHeight: 'auto',
													data: [{label:'箱',value:'箱'},{label:'盒',value:'盒'},
													{label:'个',value:'个'},{label:'件',value:'件'},
													{label:'卷',value:'卷'},{label:'斤',value:'斤'},
													{label:'包',value:'包'}]}
												}">单位</th>
											<th data-options="field:'tHisPurchases.cgPrice',width:'10%',editor:'textbox'">单价</th>
											<th data-options="field:'tHisPurchases.cgTotalprice',width:'10%',editor:'textbox'">金额</th>
											<!-- 不可编辑editor:{type:'textbox',options:{readonly:true}} -->
											<th data-options="field:'tHisPurchases.cgBatchnumber',width:'12%',editor:'textbox'">批号</th>
											<th data-options="field:'tHisPurchases.cgRemarks',width:'18%',editor:'textbox'">备注</th>
										</tr>
									</thead>
								</table>
								<div id="qxPurchaseTB" style="height: auto;padding-left: 30px;">
									<a href="javascript:void(0)" class="easyui-linkbutton"
									data-options="iconCls:'icon-add',plain:true" onclick="append()">添加一行</a>
									<a href="javascript:void(0)" class="easyui-linkbutton"
										data-options="iconCls:'icon-remove',plain:true"
										onclick="removeit()">移除一行</a> <a href="javascript:void(0)"
										class="easyui-linkbutton"
										data-options="iconCls:'icon-ok',plain:true" onclick="accept()">应用</a>
									<a href="javascript:void(0)" class="easyui-linkbutton"
										data-options="iconCls:'icon-undo',plain:true" onclick="reject()">撤销</a>
								</div>
				        	</div>
				        </div>
				    </div>
				</div>
				<hr noshade="noshade" size="1px" color="#95B8e7">
				<div style="margin:5px 10px 0px 0px;text-align: right;">
					<a id="submit-QiXiePurchase" class="easyui-linkbutton" data-options="iconCls:'icon-ok'">提交</a>
				</div>
			</div>
		</form>
	<!-- </div> -->
</body>
</html>